package com.tt.StackQueue;

import java.util.Stack;

public class MyQueue {
    /*
     * 双栈实现队列
     * 一个栈用于入队，一个栈用于出队
     * */
    private Stack<Integer> stackPush;
    private Stack<Integer> stackPop;

    public MyQueue() {
        stackPush = new Stack<>();
        stackPop = new Stack<>();
    }

    // 入队操作，将元素压入 stackPush
    public void push(int x) {
        stackPush.push(x);
    }

    // 出队操作
    public int pop() {
        if (stackPop.empty()) {
            // 将 stackPush 的元素全部倒着放入 stackPop
            while (!stackPush.empty()) {
                stackPop.push(stackPush.pop());
            }
        }
        return stackPop.pop();
    }

    // 获取队列头部元素
    public int peek() {
        if (stackPop.empty()) {
            while (!stackPush.empty()) {
                stackPop.push(stackPush.pop());
            }
        }
        return stackPop.peek();
    }

    // 判断队列是否为空
    public boolean empty() {
        return stackPush.empty() && stackPop.empty();
    }
}